<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="版本号" prop="version">
        <el-input
          v-model="queryParams.version"
          placeholder="请输入版本号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="equipmentType">
        <el-select
          v-model="queryParams.equipmentType"
          placeholder="请选择设备类型"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.app_equipment_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="app类型" prop="appType">
        <el-select
          v-model="queryParams.appType"
          placeholder="请选择app类型"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.app_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.app_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb-8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['software:version:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="versionList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="设备类型"
        align="center"
        prop="equipmentType"
        width="120"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.app_equipment_type"
            :value="scope.row.equipmentType"
          />
        </template>
      </el-table-column>
      <el-table-column label="APP类型" align="center" prop="appType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_type" :value="scope.row.appType" />
        </template>
      </el-table-column>
      <el-table-column label="APP图标" align="center" prop="icon">
        <template slot-scope="{ row }">
          <image-preview
            v-if="row.icon"
            :src="row.icon"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column label="APP名称" align="center" prop="name" />
      <el-table-column
        label="APP包名"
        align="center"
        prop="action"
        min-width="200"
      />
      <el-table-column label="APP状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="版本号" align="center" prop="version" />
      <el-table-column
        label="版本说明"
        align="center"
        prop="imprint"
        show-overflow-tooltip
        min-width="200"
      />
      <el-table-column
        label="是否强制更新"
        align="center"
        prop="isForce"
        width="120"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.whether_force_update"
            :value="scope.row.isForce"
          />
        </template>
      </el-table-column>

      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="安装包"
        align="center"
        prop="installPath"
        width="80"
        fixed="right"
      >
        <template slot-scope="{ row }">
          <el-link type="success" :href="row.installPath">点击下载</el-link>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120px"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['software:version:operate']"
            >{{ scope.row.status === "0" ? "发布" : "下架" }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['software:version:edit']"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改app版本管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :show-message="false" label-width="110px">
        <el-form-item label="设备类型" prop="equipmentType" required>
          <el-radio-group v-model="form.equipmentType">
            <el-radio
              v-for="dict in dict.type.app_equipment_type"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="app类型" prop="appType" required>
          <el-radio-group v-model="form.appType">
            <el-radio
              v-for="dict in dict.type.app_type"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="app图标" prop="icon" required>
          <ImageUpload v-model="form.icon" />
        </el-form-item>
        <el-form-item label="app名称" prop="name" required>
          <el-input v-model="form.name" placeholder="请输入app名称" />
        </el-form-item>
        <el-form-item label="app包名" prop="action" required>
          <el-input v-model="form.action" placeholder="请输入app包名" />
        </el-form-item>
        <el-form-item label="版本号" prop="version" required>
          <el-input v-model="form.version" placeholder="请输入版本号" />
        </el-form-item>
        <el-form-item label="版本说明" prop="imprint" required>
          <el-input
            type="textarea"
            :rows="3"
            v-model="form.imprint"
            placeholder="请输入版本说明"
          />
        </el-form-item>
        <el-form-item label="APP可见范围" prop="platformIds" required>
          <SelectEnterprise multiple :defaultValue.sync="form.platformIds" />
        </el-form-item>
        <el-form-item label="是否强制更新" prop="isForce" required>
          <el-radio-group v-model="form.isForce">
            <el-radio
              v-for="dict in dict.type.whether_force_update"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" prop="status" required>
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.app_status"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="安装包路径" prop="installPath">
          <file-upload v-model="form.installPath" :limit="1" :fileSize="200" />
        </el-form-item>
        <!-- <el-form-item label="wgt资源包路径" prop="wgtPath">
          <file-upload v-model="form.wgtPath" :limit="1" :fileSize="200"/>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listVersion,
  getVersion,
  delVersion,
  addVersion,
  updateVersion,
} from "@/api/saas/version";
import ImageUpload from '@/components/ImageUpload'
import SelectEnterprise from '@/views/enterprise/enterprise/selectEnterprise'
import { arrayUnique } from '@/utils/ruoyi'

export default {
  name: "Version",
  dicts: [
    "app_equipment_type",
    "app_type",
    "app_status",
    "whether_force_update",
  ],
  components: {
    ImageUpload,
    SelectEnterprise
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // app版本管理表格数据
      versionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentType: null,
        appType: null,
        version: null,
        imprint: null,
        isForce: null,
        status: null,
        installPath: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 已选择事业部
      selectEnterpriseItems: [],
    };
  },
  created() {
    this.getList();
  },
  activated() {
    this.getList();
  },
  methods: {
    /** 查询app版本管理列表 */
    getList() {
      this.loading = true;
      listVersion(this.queryParams).then((response) => {
        this.versionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        equipmentType: null,
        appType: null,
        version: null,
        imprint: null,
        isForce: null,
        status: "0",
        installPath: null,
        wgtPath: null,
        createTime: null,
        updateTime: null,
        icon: null,
        name: null,
        action: null,
        platformIds: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getVersion(id).then((response) => {
        this.form = response.data;
        this.form.platformIds = response.data.platformIds ? response.data.platformIds.split(',') : [];
        this.form.platformIds = this.form.platformIds.map(Number);
        this.open = true;
        this.title = "编辑";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log();
      this.$refs["form"].validate((valid) => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(this.form));
          params.platformIds = params.platformIds.join(',');
          if (params.id) {
            updateVersion(params).then((response) => {
              this.$modal.msgSuccess("编辑成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVersion(params).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm(
        "是否确认" + (row.status === "0" ? "发布" : "下架") + "？",
        "系统确认",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        const status = row.status;
        row.status = row.status === "0" ? "1" : "0";
        updateVersion(row).then((response) => {
          this.getList();
          this.$modal.msgSuccess((status === "0" ? "发布" : "下架") + "成功");
        });
      });
    }
  },
};
</script>
